package com.tom.mybatis.session;

import com.tom.mybatis.config.Configuration;
import com.tom.mybatis.config.MapperRegistory;
import com.tom.mybatis.executor.Executor;
import com.tom.mybatis.mapper.MapperProxy;

import java.lang.reflect.Proxy;

/**
 * @author : tom
 * @project: tom-mybatis
 * @date : 2018/11/7 14:51
 */
public class SqlSession {

    private Configuration configuration;

    private Executor executor;

    private MapperRegistory mapperRegistory=new MapperRegistory();

    public MapperRegistory getMapperRegistory() {
        return mapperRegistory;
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    public SqlSession(Configuration configuration, Executor executor) {
        this.configuration = configuration;
        this.executor = executor;
    }

    public <T> T getMapper(Class<T> clazz) {
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(),
                new Class[]{clazz}, new MapperProxy(this, clazz));
    }

    public <T> T selectOne(MapperRegistory.MapperDate mapperData, Object parameter) throws Exception {
        return executor.query(mapperData, parameter);
    }
}
